import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Logger,
  LoggerService,
  Inject,
  BadRequestException,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { isNumber } from 'src/utils/util';

@ApiTags('用户')
@Controller('users')
export class UsersController {
  constructor(
    private readonly usersService: UsersService,
    private readonly logger: Logger,
  ) {
    this.logger.log('UserController Init----');
  }

  @Get()
  getUsers() {
    this.logger.log('请求getUsers成功');
    this.logger.warn('请求getUsers成功');
    // this.logger.error('请求getUsers成功');
    return this.usersService.findAll();
  }

  @Post()
  @ApiOperation({
    summary: '添加用户',
  })
  create(@Body() createUserDto: CreateUserDto) {
    return this.usersService.create(createUserDto);
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    if (!isNumber(id)) throw new BadRequestException('无效的id');
    return this.usersService.findOne(+id);
  }
}
